//
//  LXBaseViewController.swift
//  LXTXShop
//
//  Created by apple on 2018/4/3.
//  Copyright © 2018年 LX. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift

class LXBaseViewController: UIViewController {
    let disposeBag = DisposeBag()
    
    init() {
        super.init(nibName: nil, bundle: nil)
    }
    
    required convenience init?(coder aDecoder: NSCoder) {
        self.init()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        initData()
        createViews()
        createEvents()
        loadData()
    }
    
    func initData() {
        
    }
    
    func createViews() {
        
    }
    
    func createEvents() {
        
    }
    
    func loadData() {
        
    }
    
    //隐藏标题
    func titleHide() {
        if (self.navigationController?.navigationBar.subviews.count)!>2 {
            self.navigationController?.navigationBar.subviews[1].isHidden = true
        }
    }
    
    func rightButton(_ imageName:String){
        
        let btn = UIButton(frame:CGRect(x: 0, y: 0, width: 25, height: 25))
        btn.setBackgroundImage(UIImage(named: imageName), for: UIControlState())
        btn.addTarget(self, action: #selector(self.rightButtonAction), for: UIControlEvents.touchUpInside)
        let rightBtn  = UIBarButtonItem(customView:btn)
        self.navigationItem.rightBarButtonItem = rightBtn
    }
    
    func rightTitleButton(_ title:String) {
        
        let btn = UIButton(frame:CGRect(x: 0, y: 0, width: 50, height: 25))
        btn.setTitle(title, for: UIControlState())
        btn.addTarget(self, action: #selector(self.rightButtonAction), for: UIControlEvents.touchUpInside)
        btn.titleLabel?.font = FONT(14)
        
        let w = labelWidth((btn.titleLabel?.text)!, font: FONT(14), height: 25)
        btn.width = w
        let rightBtn = UIBarButtonItem.init(customView: btn)
        self.navigationItem.rightBarButtonItem = rightBtn
        
    }
    
    func leftButton(_ imageName:String){
        
        let btn = UIButton(frame:CGRect(x: 0, y: 0, width: 25, height: 25))
        btn.setBackgroundImage(UIImage(named: imageName), for: UIControlState())
        btn.addTarget(self, action: #selector(self.leftButtonAction), for: UIControlEvents.touchUpInside)
        let leftBtn  = UIBarButtonItem(customView:btn)
        self.navigationItem.leftBarButtonItem = leftBtn
    }
    
    func leftTitleButton(_ title:String) {
        
        let btn = UIButton(frame:CGRect(x: 0, y: 0, width: 50, height: 25))
        btn.setTitle(title, for: UIControlState())
        btn.addTarget(self, action: #selector(self.leftButtonAction), for: UIControlEvents.touchUpInside)
        btn.titleLabel?.font = FONT(14)
        
        let w = labelWidth((btn.titleLabel?.text)!, font: FONT(14), height: 25)
        btn.width = w
        let leftBtn = UIBarButtonItem.init(customView: btn)
        self.navigationItem.leftBarButtonItem = leftBtn
        
    }
    
    @objc func rightButtonAction() {}
    @objc func leftButtonAction() {}

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}
